package com.yunbao.hujin.mymvpdemo3.mvp;

import android.os.Handler;
import android.util.Log;

import com.yunbao.hujin.mymvpdemo3.mvp.base.AbstractMvpPersenter3;
import com.yunbao.hujin.mymvpdemo3.request.WeatherBean;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * P层-特点:需要持有M层和V层 on 2017/12/7.
 * 4.修改Presenter和Activity中的代码，各自继承自己的基类并去除重复代码
 */

public class RequestPresenter3 extends AbstractMvpPersenter3<RequestView3>{

//    private RequestView3 mView;
    private RequestMode3 mMode;

    public RequestPresenter3() {
        this.mMode = new RequestMode3();
    }

    /**
     * 处理数据逻辑层
     */
    public void clickRequest(final String cityId) {
        //获取View
        if (getmMvpView() != null) {
            //请求时显示加载
            getmMvpView().requestLoading();
        }

            //模拟耗时，可以展示出loading
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mMode.request(cityId, new Callback<WeatherBean>() {
                        @Override
                        public void onResponse(Call<WeatherBean> call, Response<WeatherBean> response) {
                            //判断View是否为空
                            if (getmMvpView() != null)
                                getmMvpView().resultSuccess(response.body());    //view展示数据
                        }

                        @Override
                        public void onFailure(Call<WeatherBean> call, Throwable t) {
                            if (getmMvpView() != null)
                                getmMvpView().resultFailure(Log.getStackTraceString(t));
                        }
                    });
                }
            }, 1000);
    }

    /**
     * 取消网络请求
     */
    public void interruptHttp() {
        mMode.interruptHttp();
    }

}
